package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Department;
import unipi.database.ErrorCodes;
import unipi.database.Rights;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class NewDepartment extends HttpServlet
{
private static final long serialVersionUID = 1L;
	
	public NewDepartment()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String validateInputs(String parentDptID)
	{
		if(!Validator.isNumber(parentDptID) && !parentDptID.equals("none"))
			return "Το επιλεγμένο τμήμα δεν είναι έγκυρο.";
		
		return null;		
	}
	
	private String getPage(String uid, String message)
	{
		String content = "<h1>Δημιουργία Τμήματος</h1>";
		
		List<List<String>> dpts = Rights.getDpts(uid, Rights.RightCols.DptRights);
		
		content += HTMLPages.getSubmitForm("NewDepartment", 
							new String[]
									{
										"Όνομα",
										"Περιγραφή",
										"Υποτμήμα του"
									}, 
							new String[]
									{
										"<input class=\"mediumInput\" name=\"name\" type=\"text\" value=\"\" />",
										"<textarea  class=\"txtarea\" name=\"desc\" rows=\"6\" cols=\"40\" ></textarea>",
										"<div class=\"ddldivstyle\">" + 
										HTMLPages.getDropDownList(dpts, "parentDptID", "none", "--Επιλέξτε τμήμα--")+"</div>"
									}, "Δημιουργία") + message;
		
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String name = request.getParameter("name");
			String desc = request.getParameter("desc");
			String parID = request.getParameter("parentDptID");
			
			String validation = validateInputs(parID);
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			if(validation == null)
			{
				if(name != null && desc != null)
				{
					name = Validator.escape(name);
					desc = Validator.escape(desc);
					
					ErrorCodes newDpt;
					
					if(parID.equals("none"))
						newDpt = Department.AddAndRank(name, desc, null, uid);
					else					
						newDpt = Department.AddAndRank(name, desc, parID, uid);
					
					if(newDpt != ErrorCodes.OK)
					{
						out.println(getPage(uid, "Παρουσιάστκηκε πρόβλημα κατα την καταχώρηση του τμήματος."));					
					}
					else
					{
						out.println(getPage(uid, "Το τμήμα καταχωρήθηκε με επιτυχία."));				
					}
				}
				else
					out.println(getPage(uid, "Παρουσιάστκηκε πρόβλημα κατα την καταχώρηση του τμήματος."));	
				
			}
			else
			{
				out.println(getPage(uid, validation));					
			}	
			
			out.close();
		}

	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, ""));
			
			out.close();			
		}
	}

}
